/**
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 */
package com.ce.pms.house.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.house.dao.IProjectDao;
import com.ce.pms.house.model.Environment;
import com.ce.pms.house.model.Layout;
import com.ce.pms.house.model.ProjectDomain;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述: ProjectDaoImpl
 *
 * @author 付湘辉 Created on： 2008-6-25
 * @author 付湘辉 update on： 2008-6-25
 */

public class ProjectDaoImpl extends PmsBaseDaoImpl<ProjectDomain> implements
		IProjectDao {

	/**
	 * @roseuid 4861E7710148
	 */
	public ProjectDaoImpl() {

	}
	/**
	 * 方法描述: 根据楼栋ID查询相关建设期
	 * @author 胡煦
	 * Create on:2008-8-21
	 * @param buildingId
	 * @return ProjectDomain
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public ProjectDomain queryProjectByBuildingId(Long buildingId){
		String queryStr = "from ProjectDomain pj left outer join BuildingDomain bd on pj.id = bd.project.id where pj.id =?";
		return (ProjectDomain) this.createQuery(queryStr, buildingId);
	}
	/**
	 * @param project
	 * @return ProjectDomain
	 * @roseuid 4861E7710177
	 */
	public ProjectDomain updateProject(ProjectDomain project) {
		return null;
	}

	/**
	 * 方法描述: 分页查询方法
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param qc
	 * @param pageNo
	 * @param pageSize
	 * @roseuid 4861E77101E4
	 * @return page Edit author: Edit on: 修改原因：
	 */

	public Page queryPageProjects(Long id, int pageNo, int pageSize) {
		String queryStr = "from ProjectDomain pj where pj.area.keyword=? order by pj.sysInfo.sysDate";
		return this.pagedQueryByPageNo(queryStr, pageNo, pageSize,
				new Object[] { id });
	}
	/**
	 * 功能描述:根据指定期的名字查询期数实体<br>
	 *
	 * @param projectName
	 * @param ProjectDomain
	 * @return Create author:胡煦 Create on:2008-7-14 Edit author: Edit on: Why:
	 */
	public ProjectDomain getProjectByName(String projectName){
		String queryStr = "from ProjectDomain pj where pj.name =?";
		Object object = this.createQuery(queryStr, new Object[] { projectName }).uniqueResult();
		return  (ProjectDomain) object;
	}
	/**
	 * 功能描述:根据指定期的名字查询记录数<br>
	 * 注：新建判断name的唯一性时，projectId为null；修改判断name的唯一性时，则需要传入projectId<br>
	 *
	 * @param projectName
	 * @param projectId
	 * @return Create author:胡煦 Create on:2008-7-14 Edit author: Edit on: Why:
	 */
	public Long checkProjectName(String projectName, String projectId) {
		StringBuffer hql = new StringBuffer();
		List list = new ArrayList();
		list.add(projectName);
		hql.append("select count(*) FROM ProjectDomain project where project.name = ? ");
		if (projectId != null) {
			hql.append(" project.id <> ?");
			list.add(projectId);
		}
		Query query = this.createQuery(hql.toString(), list.toArray());
		Object result = query.uniqueResult();

		return (result == null ? null : (Long) result);

	}

	/**
	 * 功能描述:根据指定的建设规划的土地宗号查询记录数<br>
	 * 注：新建判断name的唯一性时，layoutId为null；修改判断name的唯一性时，则需要传入layoutId<br>
	 *
	 * @param layoutName
	 * @param projectId
	 * @param layoutId
	 * @return Create author:胡煦 Create on:2008-7-14 Edit author: Edit on: Why:
	 */
	public Long checkLayoutName(String layoutName, Long projectId, Long layoutId) {
		StringBuffer hql = new StringBuffer();
		List list = new ArrayList();
		list.add(layoutName);
		list.add(projectId);
		hql
				.append("select count(*) FROM ProjectDomain project where project.layout.glebeNo = ? and project.id = ?");
		if (layoutId != null) {
			hql.append(" and project.layout.id <> ?");
			list.add(layoutId);
		}
		Query query = this.createQuery(hql.toString(), list.toArray());
		Object result = query.uniqueResult();

		return (result == null ? null : (Long) result);

	}

	/**
	 * 功能描述:根据指定配套设施名字查询记录数<br>
	 * 注：新建判断name的唯一性时，environmentId为null；修改判断name的唯一性时，则需要传入environmentId<br>
	 *
	 * @param environmentName
	 * @param projectId
	 * @param environmentId
	 * @return Create author:胡煦 Create on:2008-7-14 Edit author: Edit on: Why:
	 */
	public Long checkEnvironmentName(String environmentName, Long projectId,
			Long environmentId) {
		StringBuffer hql = new StringBuffer();
		List list = new ArrayList();
		list.add(environmentName);
		list.add(projectId);
		hql
				.append("select count(*) FROM Environment environment where environment.name = ? and environment.project.id = ?");
		if (environmentId != null) {
			hql.append(" and environment.id <> ?");
			list.add(environmentId);
		}
		Query query = this.createQuery(hql.toString(), list.toArray());
		Object result = query.uniqueResult();

		return (result == null ? null : (Long) result);

	}

	/**
	 *
	 * 功能描述: 根据projectId检索ProjectDomain
	 *
	 * @author 付湘辉 Created on:2008-7-7
	 *
	 * 修改原因：
	 * @param id
	 * @return ProjectDomain 返回结果
	 * @roseuid 48479CD701B5
	 */
	public ProjectDomain getProject(Long id) {
		return super.findById(id);
	}

	/**
	 * @param o
	 * @return ProjectDomain
	 * @roseuid 4861E771033C
	 */
	public ProjectDomain saveProject(ProjectDomain o) {
		return null;
	}

	/**
	 *
	 * 功能描述: 根据楼盘id得到期数列表
	 *
	 * @author 付湘辉 Created on:2008-7-8
	 *
	 * 修改原因：
	 * @param areaId
	 * @return List<ProjectDomain> 返回结果
	 */
	public List<ProjectDomain> getProjectDomainListByAreaId(Long areaId) {
		List<ProjectDomain> list = new ArrayList<ProjectDomain>();

		String queryStr = "FROM ProjectDomain pd WHERE pd.area.id=? ";

		list = super.createQuery(queryStr, areaId).list();

		return list;
	}

}
